import { initialSchema, formatForArrayTable } from '@/components/schema-form-render/utils';
import { TableOperate } from '@/components/SelfIcon';
import { getButtons } from '@/utils/with-btns';
import { Popconfirm } from 'antd';
import React, { useState, useEffect } from 'react';
import { registerListTableEvent, registerListButtonEvent } from './utils';

/**
 * 所有按钮初始化
 * @param hooks context
 * @param events  注册事件
 * @param params 执行事件参数
 * @returns  void
 */
export function useEventsRegister(hooks:any , events: any, params: any, eventMap?:any) {
  const { loading, buttons, tableButtons } = params;
  useEffect(() => {
    if (!events) return
    if (events?.tableButtonsEvents) {
      registerListTableEvent(hooks?.current?.tableButtonsEvents, events?.tableButtonsEvents, params);
    }
    if (events?.buttonsEvents) {
      registerListButtonEvent(hooks?.current?.buttonsEvents, events?.buttonsEvents, params);
    }
  }, []);
  
  // 生成新增按钮
  const getTableTopButton = () =>
    getButtons({ loading, buttons }, hooks.current.buttonsEvents);

  // 列表行项目操作按钮
  // @ts-ignore
  const operations = (id: string, row: any,_, rowIndex: number) =>
  tableButtons?.map((o: any, index: any) => {
    // 如果是启用
    if (row?.status === 'ENABLE' && (o.key === 'icon-shanchu' || o.key === 'icon-qiyong')) {
      return null;
    }
    // 如果是禁用
    if (row?.status === 'DISABLE' && o.key === 'icon-jinyong') {
      return null;
    }
    if (['icon-shanchu', 'icon-qiyong', 'icon-jinyong', 'shanchu'].includes(o.key)) {
      return (
        <Popconfirm
          title="是否确定？"
          onConfirm={() => {
            eventMap[o?.props?.hook]?eventMap[o?.props?.hook](id, row,rowIndex):hooks.current.tableButtonsEvents[o?.props?.hook](id, row, rowIndex);
          }}
        >
          <TableOperate type={o.key} text={o.label} key={index}/>
        </Popconfirm>
      );
    }
    return (
      <TableOperate
      key={o.key}
      type={`icon-${o.key}`}
      text={o.label}
      onClick={() => {
        eventMap[o?.props?.hook]?eventMap[o?.props?.hook](id, row):hooks.current.tableButtonsEvents[o?.props?.hook](id, row);
      }}
      />
    );
  });
  return {
    getTableTopButton,
    operations
  }
}

/**
 * 数据初始化
 * @param metasConfig 传入自己构建配置信息
 * @param remoteMetasConfig 通过 pageCode 拿
 * @param type 单据类型
 * @param needGroup 是否需要分组
 * @returns 初始化后 的配置信息
 */
export function useInitData(metasConfig: any, remoteMetasConfig:any, type:'NORMAL'|'SEARCH' = 'SEARCH', needGroup: boolean = false) {
  const [smConfigs, setConfigs] = useState<any>({})
  useEffect(async() => {
    if (metasConfig?.code || remoteMetasConfig?.code) {
      const sss:any = initialSchema(
        metasConfig || remoteMetasConfig, type, needGroup
      )
      if(sss?.ArrayTableList){
        await formatForArrayTable(sss.ArrayTableList, sss.schema, 'NORMAL')
        setConfigs(sss);
      }else {
        setConfigs(sss);
      }
    }
  }, [JSON.stringify(metasConfig), remoteMetasConfig?.code])
  return smConfigs
}